import { request } from '@/utils/request';

// 登录
export const loginPwd = async (data: { username?: string; email?: string; phone?: string; password: string }) => {
  return request.post({ url: '/login/username', data });
};



// 退出登录
export const logout = async () => {
  return request.get({ url: '/login/logout' });
};

//清除缓存
export const clearCache = async () => {
  return request.get({ url: '/currentUser/clearCache' });
};

//当前登陆人信息
export const currentUser = async () => {
  return request.get({ url: '/currentUser/info' });
};

//获取当前登录用户的角色列表和权限列表
export const getPermsApi = async () => {
  return request.get({ url: '/currentUser/getPerms' });
};

//修改个人信息
export const updateUserApi = async (data: any) => {
  return request.post({ url: '/currentUser/updateUser', method: 'post', data });
};

// 生成微信登录二维码
export const generateQRCode = async () => {
  try {
    const response = await request.get({
      url: '/login/generateQRCode',
      responseType: 'blob' // 确保以二进制形式接收图片
    });
    const uuid = response.headers['x-uuid'];
    if (!uuid) {
      console.error('UUID not found in response headers');
      throw new Error('UUID not found in response headers');
    }
    const imageBlob = new Blob([response.data], { type: 'image/png' });
    const imageUrl = URL.createObjectURL(imageBlob);
    return {
      imageUrl,
      uuid
    };
  } catch (error) {
    console.error('生成二维码失败:', error);
    throw new Error('生成二维码失败');
  }
};


export const checkQRCodeStatus = async (uuid: string) => {
  return request.get({ url: '/login/checkQRCodeStatus', params: { uuid } });
};